<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[{ name: 'box-base', title: '基础信息' },{ name: 'box-content', title: '调价信息' }]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem
            label="商品名称"
            name="matrName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.matr(selectChange)"
          />
          <customFormItem
            label="商品规格" name="matrSpec" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="供应商名称" name="supplierName" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="更新库存价格"
            name="flagStock"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.switch.name"
            help="选中: 更新相关库存，未选中: 只更新耗材价格"
          />
          <customFormItem
            label="按耗材调整"
            name="flagMatr"
            :parent="_self"
            :form="form"
            help="选中: 按照耗材调整相关库存，未选中: 按照耗材及价格调整相关库存"
            :type="constCommon.formItemType.switch.name"
          />

          <div id="box-content" class="box-title">调价前</div>
<!--          <customFormItem
            label="调价前HRP编码" name="hrpCodeBefore" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="调价后HRP编码" name="hisCodeAfter" :parent="_self" :form="form"
          />

          <customFormItem
            label="调价前HIS编码" name="hisCodeBefore" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="调价后HIS编码"
            name="hisCodeAfter"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="$refs.tableDrawerHisCode.show()"
          />-->

          <customFormItem
            label="调价前单品单价" name="skuPriceBefore" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="调价后单品单价" name="skuPriceAfter" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"
          />

          <customFormItem
            label="调价前HIS价格" name="hisPriceBefore" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="调价后HIS价格" name="hisPriceAfter" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"
          />

          <customFormItem
            label="入库价调价价格" name="skuPriceChange" :parent="_self" :form="form" disabled
          />
          <customFormItem
            label="HIS价调价价格" name="hisPriceChange" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"
          />
          <customFormItem
            label="备注" name="remark" :parent="_self" :form="form" fullWidth
          />
        </template>
      </customForm>

       <!--调价后HIS编码-->
       <tableDrawer
          ref="tableDrawerHisCode"
          refName="tableDrawerHisCode"
          viewKey="base_matr_his"
          @confirm="tableDrawerConfirm"
          loadForce
          :widthPer="80"
          :heightPx="-1"
          :widthPx="-1"
        />
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      matrs: [],
    }
  },
  methods: {
    //患者选择后
    selectChange(row, refName) {
      console.log('row', row)
      console.log('refName', refName)
      let { matrId, name, spec, model, hrpCode, skuPrice,hisCode,supplierName,supplierId, pricePatientAdult } = row
      if (refName == 'tableDrawerMatr') {
        //品名
        this.$set(this.form, 'matrId', matrId)
        this.$set(this.form, 'matrName', name)
        this.$set(this.form, 'matrSpec', spec)
        this.$set(this.form, 'matrModel', model)
        this.$set(this.form, 'hrpCodeBefore', hrpCode)
        this.$set(this.form, 'hrpCodeAfter', hrpCode)
        this.$set(this.form, 'skuPriceBefore', skuPrice)
        this.$set(this.form, 'hisCodeBefore', hisCode)
        this.$set(this.form, 'supplierName', supplierName)
        this.$set(this.form, 'supplierId', supplierId)
      }
    },
    // 选择His编码
    tableDrawerConfirm(row, refName) {
      let { hisCode, pricePatientAdult } = row
      if (refName == 'tableDrawerHisCode') {
        this.$set(this.form, 'hisCodeAfter', hisCode)
        this.$set(this.form, 'hisPriceAfter', pricePatientAdult)
      }
    },

    accAdd(arg1, arg2) {
      let r1
      let r2
      let m
      try {
        r1 = arg1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = arg2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      // eslint-disable-next-line no-restricted-properties
      m = Math.pow(10, Math.max(r1, r2))
      return ((arg1 * m - arg2 * m) / m).toFixed(2)
    },
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        this.$set(this.form, 'hisPriceChange', this.hisPriceChange)
        this.$set(this.form, 'skuPriceChange', this.skuPriceChange)
        if (this.form.skuPriceAfter < 0) {
          this.utilMessage.warning('调价后单品单价不能为负数')
          return false
        }
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (!this.form.id) {
          this.$set(this.form, 'flagStock', false)
          this.$set(this.form, 'adjustId', initData.id)
        }
      },
      afterSave: () => {
        that.propParent.fetchData()
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  computed: {
    hisPriceChange() {
      let price = this.accAdd(this.form.hisPriceAfter, this.form.hisPriceBefore)
      if (!(this.form.hisPriceAfter - this.form.hisPriceBefore)) {
        price = 0
      }
      return price
    },
    skuPriceChange() {
      let price = this.accAdd(this.form.skuPriceAfter, this.form.skuPriceBefore)
      if (!(this.form.skuPriceAfter - this.form.skuPriceBefore)) {
        price = 0
      }
      return price
    }
  },
  components: {
    customAnchor
  },
}
</script>
